package com.quasar.hpatient.dialog;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.quasar.hpatient.R;

import java.util.ArrayList;
import java.util.List;

import lib.quasar.base.dialog.BaseDialog;
import lib.quasar.context.BaseApp;
import lib.quasar.widget.menu.NavigationView;

public class ChooseSingleDialog extends BaseDialog {

    private final ArrayList<String> mDatas = new ArrayList<>();
    private final ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(BaseApp.getContext(), android.R.layout.simple_list_item_1, mDatas) {
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            final TextView text = (TextView) super.getView(position, convertView, parent);
            text.setGravity(Gravity.CENTER);
            return text;
        }
    };
    private OnDialogChangeListener listener;

    public ChooseSingleDialog(Activity activity) {
        super(activity);
    }

    @Override
    protected int initView() {
        return R.layout.layout_dialog_choose_single;
    }

    @Override
    protected void initData() {

        final NavigationView menu = findViewById(R.id.layout_dialog_type_menu);
        menu.setOnNavigationChangeListener((isBack, isMenu) -> {
            if (isBack) dismiss();
        });

        final ListView list = findViewById(R.id.dialog_health_time_list);
        list.setAdapter(mAdapter);
        list.setOnItemClickListener((parent, view, position, id) -> {
            if (null != listener) {
                final String s = mDatas.get(position);
                listener.onChange(s);
                dismiss();
            }
        });
    }

    public void setList(List<String> list) {

        if (null == list || list.size() == 0) return;
        mDatas.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    public void setOnDialogChangeListener(OnDialogChangeListener mListener) {
        this.listener = mListener;
    }

    public interface OnDialogChangeListener {
        void onChange(String message);
    }
}
